layui.use(['table', 'admin', 'ax','element','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var  element=layui.element;
    var laydate=layui.laydate;

    element.on('tab(doTabBrief)',function(data){
        console.log(data.index)
    });

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });

    //点击选择商品名称
    $('#choiceGoods').click(function () {
        layer.open({
            type: 2,
            title: '选择商品',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/hxBasicGoods/toChoiceGoods',
            end: function () {
                $("#goodsName").val(HxBasicGoodsInfoDlg.data.goodsName);//商品名称
            }
        });
    });

    /**
     * 出入库商品数量统计
     */
    var HxErrorPick = {
        tableId: "hxErrorPickTable"
    };

    /**
     * 初始化表格的列
     */
    HxErrorPick.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '',  title: '序号',type:"numbers"},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'realNum', sort: true, title: '入库数量'},
            {field: 'realNum', sort: true, title: '出库数量'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    HxErrorPick.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['storageUserName'] = $("#storageUserName").val();
        table.reload(HxErrorPick.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
                // 搜索按钮点击事件
                $('#btnSearch').click(function () {
                    HxErrorPick.search();
                });
                // 添加按钮点击事件
                $('#btnAdd').click(function () {
                    HxErrorPick.openAddDlg();
                });
                // 导出excel
                $('#btnExp').click(function () {
                    HxErrorPick.exportExcel();
                });
            }
        });
    };


    /**
     * 导出excel按钮
     */
    HxErrorPick.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxErrorPick.tableId,
        url: Feng.ctxPath + '/hxStorageOut/storageUser',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: HxErrorPick.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxErrorPick.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        HxErrorPick.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxErrorPick.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + HxErrorPick.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxErrorPick.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxErrorPick.onDeleteItem(data);
        }else if (layEvent === 'chuli'){
            HxErrorPick.onchuliItem(data);
        }
    });
});
